<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  define("MODULE_OK", 1);
  if (!defined("MAIN_UNIT")) die("h4xx0r");
  global $req;
  
  define("URL_START",    '<td class="s7"><a href="nachrichten.php?id=');
  define("PLAYER",       '<td><a href="spieler.php?uid=');

  define("TITLE",        '<td width="230" background="');
  define("DATE",         '<td width="100" class="right">');
  define("MSG_START",    '/msg/underline.gif" valign="top">');

  define("HIDDEN_C",     '<input type="hidden" name="c" value="');
  define("HIDDEN_T",     '<input type="hidden" name="t" value="');

  $page = $_GET["page"];
  if (empty($page)) $page = 0;
  $page = $page * 10;

  echo "<b>Messages</b><br/><br/>";

  $t = $req["t"];

  if (!empty($req["rem"]))
  {
    if (!empty($t)) $request["t"] = $t;
    $request["n1"] = $req["id"];
    $request["delmsg"] = "remove";
    $data = http_post("http://{$_SESSION["serv"]}/nachrichten.php", $request);
    $data = $data["body"];
    $_GET["id"] = "";
  }

  if ((empty($_GET["id"])) && (empty($req["new"])))
  {
    if ($t != 2)
    {
      print_link("main.php?mod=messages&amp;new=1".ps(), "Write", " - ");
      print_link("main.php?mod=messages&amp;t=2".ps(), "Send", "<br/><br/>");
    }
    else
    {
      print_link("main.php?mod=messages&amp;new=1".ps(), "Write", " - ");
      print_link("main.php?mod=messages&amp;t=0".ps(), "Inbox", '<br/><br/>');
    }
    if (!empty($t)) $data = http_get("http://{$_SESSION["serv"]}/nachrichten.php?s={$page}&t=$t");
    else $data = http_get("http://{$_SESSION["serv"]}/nachrichten.php?s={$page}");
    $data = $data["body"];
    //echo $data;
    if (!strpos($data, URL_START)) echo "<b>No messages</b><br/><br/>";
    else
    {
      while (True)
      {
        $data = substr($data, strpos($data, URL_START) + strlen(URL_START));
        $msgid = substr($data, 0, strpos($data, '"'));
        $data = substr($data, strpos($data, '">') + 2);
        $title = substr($data, 0, strpos($data, '</a>'));
        $temp = substr($data, strpos($data, '</a>') + 4);
        $temp = substr($temp, 0, strpos($temp, '</td>'));
        $data = substr($data, strpos($data, PLAYER) + strlen(PLAYER));
        $data = substr($data, strpos($data, '">') + 2);
        $player = substr($data, 0, strpos($data, '</a>'));
        $data = substr($data, strpos($data, '<td>') + 4);
        $date = substr($data, 0, strpos($data, '</td>'));
        print_link("main.php?mod=messages&amp;id=$msgid".ps(), $title);
        if (strpos($temp, ')') !== False) echo "<br/>Unread message";
        echo "<br/>$player $date<br/>";
        print_link("main.php?mod=messages&amp;id=$msgid&amp;rem=1&amp;t=$t".ps(), "Delete", "<br/><br/>");
        if (!strpos($data, URL_START)) break;
      }
      $p = ($page / 10) - 1;
      $n = ($page / 10) + 1;
      if (!strpos($data, '<span class="c"><b>&raquo;</b>')) $c = " - ";
      if ($page != 0) print_link("main.php?mod=messages&amp;page=$p&amp;t=$t".ps(), "Previous", $c);
      if (!strpos($data, '<span class="c"><b>&raquo;</b>')) print_link("main.php?mod=messages&amp;page=$n&amp;t=$t".ps(), "Next", "<br/><br/>");
      if ((empty($c)) && ($page != 0)) echo "<br/><br/>";
    }
  }
  elseif (!empty($req["new"]))
  {
    print_link("main.php?mod=messages&amp;t=0".ps(), "Inbox", ' - ');
    print_link("main.php?mod=messages&amp;t=2".ps(), "Send", "<br/><br/>");
    if (!empty($req["to"]))
    {
      $text = http_get("http://{$_SESSION["serv"]}/nachrichten.php?t=1");
      $text = $text["body"];
      $c = substr($text, strpos($text, HIDDEN_C) + strlen(HIDDEN_C));
      $c = substr($c, 0, strpos($c, '"'));
     /* $t = substr($text, strpos($text, HIDDEN_T) + strlen(HIDDEN_T));
      $t = substr($t, 0, strpos($t, '"'));  */
      $d = array(
        "an"      => $req["to"],
        "be"      => $req["title"],
        "message" => $req["val"],
        "c"       => $c,
        "t"       => 2
      );
      http_post("http://{$_SESSION["serv"]}/nachrichten.php", $d);
      echo "Message sent<br/><br/>";
    }

    $pfields = array(
      "to"    => '$(to)',
      "title" => '$(title)',
      "val"   => '$(val)',
      "new"   => '1',
      "mod"   => "messages"
    );

    print_form_top("main.php".ps('?'), $pfields, "get");

    ?>
    To:<br/><input type="text" name="to"/><br/>
    Title:<br/><input type="text" name="title"/><br/>
    Content:<br/><input type="text" name="val"/><br/>
    <?php
    print_form_bottom("Send");
  }
  else
  {
    print_link("main.php?mod=messages&amp;new=1".ps(), "Write", " - ");
    print_link("main.php?mod=messages&amp;t=0".ps(), "Inbox", ' - ');
    print_link("main.php?mod=messages&amp;t=2".ps(), "Send", "<br/><br/>");
    $data = http_get("http://{$_SESSION["serv"]}/nachrichten.php?id={$_GET["id"]}");
    $text = $data["body"];
    $text = substr($text, strpos($text, TITLE) + strlen(TITLE));
    $from = substr($text, strpos($text, '>') + 1);
    $from = substr($from, 0, strpos($from, '<'));
    $text = substr($text, strpos($text, DATE) + strlen(DATE));
    $date1 = substr($text, 0, strpos($text, '<'));
    $text = substr($text, strpos($text, TITLE) + strlen(TITLE));
    $title = substr($text, strpos($text, '>') + 1);
    $title = substr($title, 0, strpos($title, '<'));
    $text = substr($text, strpos($text, DATE) + strlen(DATE));
    $date2 = substr($text, 0, strpos($text, '<'));
    $message = substr($text, strpos($text, MSG_START) + strlen(MSG_START));
    $message = substr($message, 0, strpos($message, '</td>'));
    $message = str_replace('<br />', '<br/>', $message);
    echo "From: $from<br/>Title: $title<br/>$date1 $date2<br/><br/>$message<br/><br/>";
    print_link("main.php?mod=messages".ps(), "Back to messages", "<br/><br/>");
  }
  

